package com.spring.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.tool.App;
import com.spring.util.Constant;

import org.apache.http.Header;
import org.json.JSONObject;
import org.json.alipay.JSONException;

/**
 * Created by Administrator on 2016/6/27.
 * 扫码支付
 */
public class ScanPayActivity extends BaseActivity implements View.OnClickListener {
    private TextView pice,title_tv;

    private Button ok,no;
    private RelativeLayout ZFB, VX;
    private final static int SCANNIN_GREQUEST_CODE = 1;
    String username = "";
    RestClient restClient = null;
    String logintype = null;
    String succes,reg;
String  result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanpay);
        SharedPreferences userInfo = this.getSharedPreferences(
                "user_info", 0);
         reg="^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
        username = userInfo.getString("username", "");
        logintype = userInfo.getString("logintype", "");
        title_tv=(TextView)findViewById(R.id.title_tv);
          Intent intent =getIntent();
        result=intent.getStringExtra("result");

        title_tv.setText("扫描支付");
         ok= (Button) findViewById(R.id.ok);
        TextView textView =(TextView) findViewById(R.id.text);
        textView.setText("您确定要在"+result+"号消费机上进行支付吗？");
       ok.setOnClickListener(this);
        no= (Button) findViewById(R.id.not);
        no.setOnClickListener(this);
        restClient = new RestClient(this);

        ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok:
                boolean islianwang = new Constant()
                        .isConnect(this);
                if (islianwang) {
                    SendHttpPay sendhttp = new SendHttpPay(
                            App.getInstance().url
                                    + "/AddScanCodeOrder",
                            result, "支付");
                    sendhttp.run();
                } else {
                    Toast.makeText(
                            this,
                            "请检查手机是否联网！", Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            case R.id.not:
                finish();
                break;

        }
    }



        class SendHttpPay implements Runnable {
            String url;
            String deCodeStr;
            String type;

            SendHttpPay(String url, String deCodeStr, String type) {
                this.url = url;
                this.deCodeStr = deCodeStr;
                this.type = type;
            }

            @Override
            public void run() {
                RequestParams requestparams = new RequestParams(url, "POST");
                requestparams.put("LoginAccount", username);
                requestparams.put("LoginType",logintype );
                requestparams.put("DeCodeStr", deCodeStr);
                requestparams.put("TransMoney","0");
                TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                                          String responseString) {
                        // TODO Auto-generated method stub
                        Log.e("TAG", "responseString1122" + responseString);
                        try {
                            Analysis(responseString);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                                          String responseString, Throwable throwable) {
                        // TODO Auto-generated method stub
                        Log.i("TAG", "responseString22" + responseString);

                    }
                };
                try {
                    restClient.post(url, requestparams, textHttpResponseHandler);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            /**
             * json数据解析
             *
             * @param strResult
             * @throws JSONException
             */
            private void Analysis(String strResult) throws JSONException {
                /**
                 * 这里需要分析服务器回传的json格式数据，
                 */
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(strResult);
                } catch (org.json.JSONException e) {
                    e.printStackTrace();
                }
                try {
                    String returnMsg = jsonObject.getString("returnMsg");

                    if (returnMsg == null || "".equals(returnMsg)) {
                        // TODO Auto-generated method stub
                        returnMsg="支付成功";
                    }
                    new AlertDialog.Builder(ScanPayActivity.this).setTitle("系统提示")//设置对话框标题

                            .setMessage(returnMsg)//设置显示的内容

                            .setNegativeButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮

                                @Override

                                public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                finish();
                                    // TODO Auto-generated method stub

                                }

                            }).show();//在按键响应事件中显示此对话框
                } catch (org.json.JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    private boolean checkString(String s) {
        return s.matches(reg);
    }
}
